package cn.passlink.bletoothchat.SqlTables;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import cn.passlink.bletoothchat.Entity.Groups;

public class GroupsTableManager {
    private SQLiteDatabase database;

    public GroupsTableManager(SQLiteDatabase database) {
        this.database = database;
    }

    public void addGroup(Groups group) {
        ContentValues values = new ContentValues();
        values.put("groupname", group.getGroupname());
        values.put("group_id", group.getGroupid());
        values.put("members_amount", group.getMembers_amount());
        values.put("group_table_name", group.getGroup_table_name());
        database.insert("groups", null, values);
    }

    public void deleteGroup(String groupId) {
        String whereClause = "group_id" + " = ?";
        String[] whereArgs = {groupId};
        database.delete("groups", whereClause, whereArgs);
    }

    public void updateGroup(Groups group) {
        ContentValues values = new ContentValues();
        values.put("groupname", group.getGroupname());
        values.put("group_id", group.getGroupid());
        values.put("members_amount", group.getMembers_amount());
        values.put("group_table_name", group.getGroup_table_name());

        String whereClause = "groupname" + " = ?";
        String[] whereArgs = {String.valueOf(group.getGroupname())};
        database.update("groups", values, whereClause, whereArgs);
    }

    public Groups getGroupById(int id) {
        String[] projection = {"id", "groupname", "group_id", "members_amount", "group_table_name"};
        String selection = "id" + " = ?";
        String[] selectionArgs = {String.valueOf(id)};

        Cursor cursor = database.query("groups", projection, selection, selectionArgs, null, null, null);
        if (cursor.moveToFirst()) {
            Groups group = new Groups();
            group.setGroupname(cursor.getString(cursor.getColumnIndexOrThrow("groupname")));
            group.setGroupid(cursor.getString(cursor.getColumnIndexOrThrow("group_id")));
            group.setMembers_amount(cursor.getString(cursor.getColumnIndexOrThrow("members_amount")));
            group.setGroup_table_name(cursor.getString(cursor.getColumnIndexOrThrow("group_table_name")));

            cursor.close();
            return group;
        }

        cursor.close();
        return null;
    }

    public List<Groups> getAllGroups() {
        List<Groups> groupList = new ArrayList<>();

        String[] projection = {"id", "groupname", "group_id", "members_amount", "group_table_name"};

        Cursor cursor = database.query("groups", projection, null, null, null, null, null);
        while (cursor.moveToNext()) {
            Groups group = new Groups();
            group.setGroupname(cursor.getString(cursor.getColumnIndexOrThrow("groupname")));
            group.setGroupid(cursor.getString(cursor.getColumnIndexOrThrow("group_id")));
            group.setMembers_amount(cursor.getString(cursor.getColumnIndexOrThrow("members_amount")));
            group.setGroup_table_name(cursor.getString(cursor.getColumnIndexOrThrow("group_table_name")));
            groupList.add(group);
        }

        cursor.close();
        return groupList;
    }
}
